package com.appspot.moviesessions;

import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.ViewGroup;

public class GenericLoader implements Loader {

	private CursorLoaderFactory cursorLoaderFactory;
	private SimpleCursorAdapter cursorAdapter;
	private Context context;
	private Uri resourceUri;
	private String[] projection;
	private String selection;
	private String[] selectionArgs;
	private String sortOrder;

	public GenericLoader(Context context,
			CursorLoaderFactory cursorLoaderFactory,
			SimpleCursorAdapter cursorAdapter, Uri resourceUri,
			String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
		this.cursorLoaderFactory = cursorLoaderFactory;
		this.cursorAdapter = cursorAdapter;
		this.context = context;
		this.resourceUri = resourceUri;
		this.projection = projection;
		this.selection = selection;
		this.selectionArgs = selectionArgs;
		this.sortOrder = sortOrder;
	}

	@Override
	public android.support.v4.content.Loader<Cursor> onCreateLoader(int arg0,
			Bundle arg1) {
		return cursorLoaderFactory.create(context, resourceUri, projection,
				selection, selectionArgs, sortOrder);
	}

	@Override
	public void onLoadFinished(android.support.v4.content.Loader<Cursor> arg0,
			Cursor cursor) {
		cursorAdapter.swapCursor(cursor);
	}

	@Override
	public void onLoaderReset(android.support.v4.content.Loader<Cursor> arg0) {
		cursorAdapter.swapCursor(null);
	}

	public boolean areAllItemsEnabled() {
		return cursorAdapter.areAllItemsEnabled();
	}

	public int getCount() {
		return cursorAdapter.getCount();
	}

	public Object getItem(int position) {
		return cursorAdapter.getItem(position);
	}

	public long getItemId(int position) {
		return cursorAdapter.getItemId(position);
	}

	public int getItemViewType(int position) {
		return cursorAdapter.getItemViewType(position);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		return cursorAdapter.getView(position, convertView, parent);
	}

	public int getViewTypeCount() {
		return cursorAdapter.getViewTypeCount();
	}

	public boolean hasStableIds() {
		return cursorAdapter.hasStableIds();
	}

	public boolean isEmpty() {
		return cursorAdapter.isEmpty();
	}

	public boolean isEnabled(int position) {
		return cursorAdapter.isEnabled(position);
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		cursorAdapter.registerDataSetObserver(observer);
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		cursorAdapter.unregisterDataSetObserver(observer);
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return cursorAdapter.getDropDownView(position, convertView, parent);
	}

}
